import { find, instantiate, Node, Prefab } from "cc";
import { resourcesMgr } from "../scripts/framework/loader/ResourcesMgr";
import { VMCenter } from "../scripts/framework/vm/VMCenter";
import { VMEntity } from "../scripts/framework/vm/VMEntity";

export class WindowNodeR implements IMVVM{

    public node: Node;

    public name: string;

    _vmTag: string;
    _vmEntitys: VMEntity[];

    intervalTimer;
    constructor(name: string) {
        this.name = name;
        this.onCtor();
    }

    onCtor(): void {
        VMCenter.VMTag(this);
        resourcesMgr.load(this, this.name, Prefab, (err: Error, prefab: Prefab) => {
            this.node = instantiate(prefab);
            this.node.setParent(window.windowParentNode);
            let btn = find('Button', this.node)
            btn.on('click', () => {
                this.close();
            }, this);
            this.onLoaded();
        }, 'test1')
    }

   
    onLoaded(){
        this.intervalTimer = setInterval(() => {
            console.log(`AddPointWindow-> `, this._vmTag, this.name);
        }, 1000);
    }

    close(): void {
        VMCenter.remove(this);
        clearInterval(this.intervalTimer);
        resourcesMgr.releaseAll(this);
        this.node.destroy();
    }

}
